package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.business.dao.sys.SysEnumDictionaryMapper;
import com.cloud.business.entity.sys.SysEnumDictionary;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysenumdictionaryvm.SysEnumDictionaryQueryModel;
import com.cloud.business.service.sys.ISysEnumDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author dh
 * @since 2020-03-25
 */
@Service
public class SysEnumDictionaryServiceImpl extends ServiceImpl<SysEnumDictionaryMapper, SysEnumDictionary> implements ISysEnumDictionaryService {

    private final SysEnumDictionaryMapper sysEnumDictionaryMapper;

    @Autowired
    public SysEnumDictionaryServiceImpl(SysEnumDictionaryMapper sysEnumDictionaryMapper) {
        this.sysEnumDictionaryMapper = sysEnumDictionaryMapper;
    }

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysEnumDictionaryQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysEnumDictionaryQueryModel sysEnumDictionaryQueryModel, IPage<SysEnumDictionary> page, ResultMsg msg) throws Exception {
        QueryWrapper<SysEnumDictionary> sysEnumDictionaryQueryWrapper = new QueryWrapper<>();
        sysEnumDictionaryQueryWrapper.lambda()
                .and(!StringUtils.isEmpty(sysEnumDictionaryQueryModel.getQueryStr()),
                        lk -> lk.like(SysEnumDictionary::getItemName, sysEnumDictionaryQueryModel.getQueryStr())
                                .or()
                                .like(SysEnumDictionary::getTableName, sysEnumDictionaryQueryModel.getQueryStr()));
        sysEnumDictionaryMapper.selectPage(page, sysEnumDictionaryQueryWrapper);
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param sysEnumDictionary
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysEnumDictionary sysEnumDictionary, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysEnumDictionary.setCreateTime(now);
        sysEnumDictionary.setModificationTime(now);
        // 判断不能重复
        if (checkDic(sysEnumDictionary.getTableName(), sysEnumDictionary.getItemValue(), 0, msg)) {
            throw new Exception("创建失败," + msg.getMsg());
        }
        try {
            int count = sysEnumDictionaryMapper.insert(sysEnumDictionary);
        } catch (Exception e) {
            msg.setMsg("创建失败");
        }
        msg.setMsg("创建成功");
        return sysEnumDictionary.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param sysEnumDictionary
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysEnumDictionary sysEnumDictionary, ResultMsg msg) throws Exception {

        Date now = new Date();
        sysEnumDictionary.setModificationTime(now);
        // 判断不能重复
        if (checkDic(sysEnumDictionary.getTableName(), sysEnumDictionary.getItemValue(), sysEnumDictionary.getObjectId(), msg)) {
            throw new Exception("更新失败," + msg.getMsg());
        }
        int count = sysEnumDictionaryMapper.updateById(sysEnumDictionary);
        msg.setMsg("更新成功");
        return sysEnumDictionary.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysEnumDictionary getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysEnumDictionary sysEnumDictionary = sysEnumDictionaryMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysEnumDictionary;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysEnumDictionaryMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

    //region 根据TableName获取列表

    /**
     * 根据TableName获取列表
     *
     * @param TableNames
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public List<SysEnumDictionary> getListByTableNames(String TableNames, ResultMsg msg) throws Exception {

        QueryWrapper<SysEnumDictionary> sysEnumDictionaryQueryWrapper = new QueryWrapper<>();
        sysEnumDictionaryQueryWrapper.lambda()
                .in(SysEnumDictionary::getTableName, Arrays.asList(TableNames.split(",")))
                ;
        List<SysEnumDictionary> sysEnumDictionaryList = sysEnumDictionaryMapper.selectList(sysEnumDictionaryQueryWrapper);

        msg.setMsg("获取成功");
        return sysEnumDictionaryList;
    }
    //endregion

    //region 根据tableName和value判断重复

    /**
     * 根据tableName和value判断重复
     *
     * @param tableName
     * @param value
     * @param id        创建传0，修改传修改的主键
     * @param msg
     * @return 如果存在返回true，如果不存在返回false
     * @throws Exception
     */
    @Override
    public boolean checkDic(String tableName, String value, Integer id, ResultMsg msg) throws Exception {

        QueryWrapper<SysEnumDictionary> sysEnumDictionaryQueryWrapper = new QueryWrapper<>();
        sysEnumDictionaryQueryWrapper.lambda()
                .eq(SysEnumDictionary::getTableName, tableName)
                .eq(SysEnumDictionary::getItemValue, value)
                ;
        SysEnumDictionary sysEnumDictionary = sysEnumDictionaryMapper.selectOne(sysEnumDictionaryQueryWrapper);
        if (sysEnumDictionary != null) {
            if (id == 0) {
                msg.setMsg("字典已存在");
                return true;
            } else {
                if (!sysEnumDictionary.getObjectId().equals(id)) {
                    msg.setMsg("字典已存在");
                    return true;
                }
            }
        }
        msg.setMsg("字典不存在");
        return false;
    }
//endregion
}
